package com.encryption.logic.impl;

import java.util.ArrayList;
import java.util.List;

import com.encryption.dao.Condition;

public class FileLogConditionSqlParser {
	
	public static String parse(List<Condition> condList){
		List<Condition> logCondList = new ArrayList<Condition>();
		String condSql = "";
		List<Condition> fileCondList = new ArrayList<Condition>();
		
		for (Condition cond : condList){
			if(cond.getCode().startsWith("f.")){
				String code = cond.getCode();
				code = code.substring(2);
				Condition fcond = new Condition(code, cond.getOperation(), cond.getValue());
				fileCondList.add(fcond);
			}else{
				logCondList.add(cond);
			}
		}
		
		String logCondStr = ConditionSqlParser.parse(logCondList);
		String fileCondStr = ConditionSqlParser.parse(fileCondList);
		if(!"".equals(fileCondStr)){
			fileCondStr = "and file_id in (select id from file where 1=1 " + fileCondStr + " )";
		}
		
		condSql = logCondStr + fileCondStr;
		
		return condSql;
	}
}
